package com.thread.base01_thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class App3 {
    public static void main(String[] args)  {
        Callable<String> callable = () -> {
            TimeUnit.SECONDS.sleep(5);
            return Thread.currentThread().getName();
        };

        // 通过Callable创建线程
        FutureTask<String> futureTask = new FutureTask<>(callable);

        // 启动线程
        Thread thread = new Thread(futureTask);
        thread.start();

        // 获取线程执行结果
        try {
            String result = futureTask.get(); // 阻塞，等待线程执行完毕
            System.out.println(result);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

    }
}
